<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>WebRTC 音视频通话</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            background-color: #f0f2f5;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            display: grid;
            grid-template-columns: 250px 1fr;
            gap: 20px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 20px;
            height: calc(100vh - 40px);
        }

        .sidebar {
            background: #fff;
            border-right: 1px solid #ddd;
            padding: 15px;
            display: flex;
            flex-direction: column;
            height: 100%;
        }

        #userId {
            font-size: 16px;
            font-weight: bold;
            color: #1a73e8;
            padding: 10px;
            background: #e8f0fe;
            border-radius: 8px;
            margin-bottom: 15px;
        }

        .online-users {
            flex: 1;
            overflow-y: auto;
        }

        .online-users h2 {
            font-size: 14px;
            color: #5f6368;
            margin-bottom: 10px;
            padding: 5px;
            background: #f8f9fa;
            border-radius: 4px;
        }

        .user-list {
            list-style: none;
        }

        .user-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 8px;
            border-bottom: 1px solid #eee;
            transition: all 0.2s;
        }

        .user-item:hover {
            background-color: #f8f9fa;
            border-radius: 4px;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .user-status {
            display: none;
        }

        .call-button {
            background: none;
            border: none;
            color: #1a73e8;  /* 默认蓝色 - 可以通话 */
            cursor: pointer;
            padding: 5px;
            transition: all 0.2s;
            font-size: 18px;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .call-button:hover {
            background-color: rgba(26, 115, 232, 0.1);
            transform: scale(1.1);
        }

        /* 通话中状态 */
        .call-button.busy {
            color: #ea4335;  /* 红色 - 通话中 */
            animation: pulse 1.5s infinite;
        }

        /* 呼吸灯动画 */
        @keyframes pulse {
            0% {
                transform: scale(1);
                opacity: 1;
            }
            50% {
                transform: scale(1.1);
                opacity: 0.7;
            }
            100% {
                transform: scale(1);
                opacity: 1;
            }
        }

        .call-button.busy:hover {
            background-color: rgba(234, 67, 53, 0.1);
            animation: none;
            transform: scale(1.1);
        }

        /* 禁用状态 */
        .call-button:disabled {
            color: #ccc;  /* 灰色 - 禁用状态 */
            cursor: not-allowed;
            transform: none;
            background: none;
            animation: none;
        }

        .call-button:disabled:hover {
            background: none;
            transform: none;
        }

        /* 保持通话中状态的动画，即使按钮被禁用 */
        .call-button:disabled.busy {
            color: #ea4335;
            animation: pulse 1.5s infinite;
        }

        .main-content {
            padding: 15px;
            display: grid;
            grid-template-rows: 1fr auto;
            height: 100%;
            gap: 15px;
        }

        .content-wrapper {
            display: grid;
            grid-template-columns: 1fr 300px;
            gap: 15px;
        }

        .video-container {
            display: grid;
            grid-template-rows: repeat(2, 1fr);
            gap: 15px;
        }

        .chat-container {
            background: white;
            border-radius: 8px;
            border: 1px solid #ddd;
            display: flex;
            flex-direction: column;
        }

        .chat-messages {
            flex: 1;
            overflow-y: auto;
            padding: 10px;
        }

        .chat-input-area {
            border-top: 1px solid #ddd;
            padding: 10px;
            display: flex;
            gap: 10px;
            position: relative;
        }

        .emoji-button {
            background: none;
            border: none;
            color: #666;
            cursor: pointer;
            padding: 8px;
            font-size: 20px;
            transition: color 0.2s;
        }

        .emoji-button:hover {
            color: #1a73e8;
        }

        .emoji-picker {
            position: absolute;
            bottom: 100%;
            left: 0;
            z-index: 1000;
        }

        .chat-input {
            flex: 1;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: none;
        }

        .chat-send-button {
            background-color: #1a73e8;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
        }

        .controls-container {
            display: flex;
            justify-content: center;
            gap: 10px;
            padding: 10px;
            background: rgba(0, 0, 0, 0.05);
            border-radius: 8px;
        }

        .control-button {
            background: white;
            border: none;
            padding: 10px;
            border-radius: 50%;
            cursor: pointer;
            width: 50px;
            height: 50px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
            color: #5f6368;
            transition: all 0.2s;
        }

        .control-button:hover {
            background: #f1f3f4;
        }

        .control-button.active {
            background: #1a73e8;
            color: white;
        }

        .control-button.end-call {
            background: #ea4335;
            color: white;
            display: none;
        }

        .control-button.end-call:hover {
            background: #d33828;
        }

        .control-button.end-call.active {
            display: flex;
        }

        .message {
            margin-bottom: 10px;
            padding: 8px 12px;
            border-radius: 12px;
            max-width: 80%;
            word-break: break-word;
        }

        .message.sent {
            background: #e3f2fd;
            margin-left: auto;
            border-bottom-right-radius: 4px;
        }

        .message.received {
            background: #f5f5f5;
            margin-right: auto;
            border-bottom-left-radius: 4px;
        }

        .message .content {
            font-size: 14px;
            line-height: 1.4;
        }

        .message .sender {
            font-size: 12px;
            color: #666;
            margin-bottom: 4px;
        }

        .video-wrapper {
            position: relative;
            width: 100%;
            height: 0;
            padding-bottom: 56.25%; /* 16:9 比例 */
            background: #000;
            border-radius: 8px;
            overflow: hidden;
        }

        video {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .video-controls {
            position: absolute;
            bottom: 10px;
            left: 50%;
            transform: translateX(-50%);
            display: flex;
            gap: 10px;
            background: rgba(0, 0, 0, 0.5);
            padding: 5px 10px;
            border-radius: 20px;
        }

        button {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.2s;
            display: flex;
            align-items: center;
            gap: 5px;
        }

        #startButton {
            background-color: #1a73e8;
            color: white;
        }

        #startButton:hover {
            background-color: #174ea6;
        }

        #callButton {
            background-color: #34a853;
            color: white;
        }

        #callButton:hover {
            background-color: #2d8a47;
        }

        #hangupButton {
            background-color: #ea4335;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 20px;
            cursor: pointer;
            display: none;
        }

        #hangupButton.active {
            display: block;
        }

        button:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }

        .hidden {
            display: none;
        }

        /* 通话邀请对话框 */
        .dialog-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.5);
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 1000;
        }

        .dialog {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            text-align: center;
        }

        .dialog h3 {
            margin-bottom: 15px;
            color: #1a73e8;
        }

        .dialog-buttons {
            display: flex;
            gap: 10px;
            justify-content: center;
            margin-top: 15px;
        }

        .accept-button {
            background-color: #34a853;
            color: white;
        }

        .reject-button {
            background-color: #ea4335;
            color: white;
        }

        /* 添加通话提示工具提示 */
        .call-button.busy::before {
            content: attr(data-tooltip);
            position: absolute;
            bottom: 100%;
            left: 50%;
            transform: translateX(-50%);
            padding: 5px 10px;
            background: rgba(0, 0, 0, 0.8);
            color: white;
            border-radius: 4px;
            font-size: 12px;
            white-space: nowrap;
            visibility: hidden;
            opacity: 0;
            transition: all 0.2s;
        }

        .call-button.busy:hover::before {
            visibility: visible;
            opacity: 1;
        }

        /* 添加视频标签样式 */
        .video-label {
            position: absolute;
            left: 10px;
            top: 10px;
            background: rgba(0, 0, 0, 0.6);
            color: white;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            z-index: 1;
        }

        /* 添加消息动画 */
        @keyframes messageIn {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .message {
            animation: messageIn 0.3s ease-out;
        }

        .whiteboard-container {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            pointer-events: none;
            z-index: 2;
        }

        .whiteboard-canvas {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            pointer-events: all;
        }

        .whiteboard-controls {
            position: absolute;
            top: 10px;
            right: 10px;
            display: flex;
            gap: 5px;
            background: rgba(0, 0, 0, 0.5);
            padding: 5px;
            border-radius: 5px;
            z-index: 3;
            display: none;
        }

        .whiteboard-controls.active {
            display: flex;
        }

        .whiteboard-button {
            background: white;
            border: none;
            width: 30px;
            height: 30px;
            border-radius: 4px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 14px;
            color: #333;
        }

        .whiteboard-button.active {
            background: #1a73e8;
            color: white;
        }

        .color-picker {
            width: 30px;
            height: 30px;
            padding: 0;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .line-width-control {
            width: 80px;
            height: 30px;
            margin: 0 5px;
            vertical-align: middle;
        }

        .video-wrapper:-webkit-full-screen {
            width: 100vw;
            height: 100vh;
        }

        .video-wrapper:-moz-full-screen {
            width: 100vw;
            height: 100vh;
        }

        .video-wrapper:-ms-fullscreen {
            width: 100vw;
            height: 100vh;
        }

        .video-wrapper:fullscreen {
            width: 100vw;
            height: 100vh;
        }

        .whiteboard-controls {
            display: flex;
            align-items: center;
            margin-top: 10px; /* 添加一些间距 */
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="sidebar">
            <div id="userId">你的ID: </div>
            <div class="online-users">
                <h2><i class="fas fa-users"></i> 在线用户</h2>
                <ul class="user-list" id="userList">
                    <!-- 用户列表将通过 JavaScript 动态添加 -->
                </ul>
            </div>
        </div>
        <div class="main-content">
            <div class="content-wrapper">
                <div class="video-container">
                    <div class="video-wrapper">
                        <div class="video-label">我</div>
                        <video id="localVideo" autoplay playsinline muted></video>
                        <div class="whiteboard-container" id="whiteboardContainer">
                            <canvas class="whiteboard-canvas" id="whiteboardCanvas"></canvas>
                        </div>
                    </div>
                    <div class="video-wrapper">
                        <div id="remoteVideoLabel" class="video-label" style="display: none;">对方</div>
                        <video id="remoteVideo" autoplay playsinline></video>
                    </div>
                </div>
                <div class="chat-container">
                    <div class="chat-messages" id="chatMessages">
                        <!-- 消息将通过 JavaScript 动态添加 -->
                    </div>
                    <div class="chat-input-area">
                        <button class="emoji-button" id="emojiButton" title="表情">
                            <i class="far fa-smile"></i>
                        </button>
                        <textarea class="chat-input" id="chatInput" placeholder="输入消息..." rows="2"></textarea>
                        <button class="chat-send-button" id="sendMessageButton">
                            <i class="fas fa-paper-plane"></i>
                        </button>
                    </div>
                </div>
            </div>
            <div class="controls-container">
                <button class="control-button" id="screenShareButton" title="屏幕共享">
                    <i class="fas fa-desktop"></i>
                </button>
                <button class="control-button" id="micButton" title="麦克风">
                    <i class="fas fa-microphone"></i>
                </button>
                <button class="control-button" id="cameraButton" title="摄像头">
                    <i class="fas fa-video"></i>
                </button>
                <button class="control-button end-call" id="hangupButton" title="结束通话">
                    <i class="fas fa-phone-slash"></i>
                </button>
                <div class="whiteboard-controls" id="whiteboardControls">
                    <button class="whiteboard-button" id="penButton" title="画笔">
                        <i class="fas fa-pen"></i>
                    </button>
                    <button class="whiteboard-button" id="eraserButton" title="橡皮擦">
                        <i class="fas fa-eraser"></i>
                    </button>
                    <input type="color" class="color-picker" id="colorPicker" value="#ff0000" title="选择颜色">
                    <button class="whiteboard-button" id="clearButton" title="清空白板">
                        <i class="fas fa-trash-alt"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- 通话邀请对话框模板 -->
    <template id="call-dialog-template">
        <div class="dialog-overlay">
            <div class="dialog">
                <h3><i class="fas fa-phone-volume"></i> 视频通话邀请</h3>
                <p><span class="caller-name"></span> 邀请你进行视频通话</p>
                <div class="dialog-buttons">
                    <button class="accept-button">
                        <i class="fas fa-phone"></i> 接受
                    </button>
                    <button class="reject-button">
                        <i class="fas fa-phone-slash"></i> 拒绝
                    </button>
                </div>
            </div>
        </div>
    </template>

    <script src="webrtc.js"></script>
</body>
</html> 